package my.org.swing.animate;

import java.util.concurrent.TimeUnit;
import org.jdesktop.core.animation.timing.Animator;
import org.jdesktop.core.animation.timing.PropertySetter;
import org.jdesktop.core.animation.timing.TimingTarget;
import org.jdesktop.swing.animation.timing.sources.SwingTimerTimingSource;

public class MyAnimator {

    private Animator timer;

    private SwingTimerTimingSource source;

    public static MyAnimator newAnimator(int duration, Object source, String property, Object... values) {
        TimingTarget listener = PropertySetter.getTarget(source, property, values);
        return newAnimator(duration, listener);
    }

    public static MyAnimator newAnimator(int duration, TimingTarget listener) {
        return new MyAnimator(duration, listener);
    }

    public MyAnimator(int duration, TimingTarget listener) {
        source = new SwingTimerTimingSource(duration / 60, TimeUnit.MILLISECONDS);
        timer = new Animator.Builder(source).setDuration(duration, TimeUnit.MILLISECONDS).addTarget(listener).build();
    }

    public void addTimingListener(TimingTarget target) {
        timer.addTarget(target);
    }

    public void start() {
        source.init();
        timer.start();
    }

    public boolean isRunning() {
        return timer.isRunning();
    }

    public void stop() {
        timer.stop();
    }
}
